package context.render.tiu.sampler;

import context.util.Data;
import java.nio.IntBuffer;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL12;
import org.lwjgl.opengl.GL33;

public class Sampler
{
    
    public static final Sampler LINEAR = new Sampler();
    
    public static final Sampler NEAREST = new Sampler();
    
    static
    {
        Sampler.LINEAR.param(GL11.GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE);
        Sampler.LINEAR.param(GL11.GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE);
        Sampler.LINEAR.param(GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR_MIPMAP_LINEAR);
        Sampler.LINEAR.param(GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
        Sampler.LINEAR.param(GL12.GL_TEXTURE_MAX_LOD, 4);
        
        Sampler.NEAREST.param(GL11.GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE);
        Sampler.NEAREST.param(GL11.GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE);
        Sampler.NEAREST.param(GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST_MIPMAP_LINEAR);
        Sampler.NEAREST.param(GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
        Sampler.NEAREST.param(GL12.GL_TEXTURE_MAX_LOD, 4);
    }
    
    
    public int mID;
    
    
    public Sampler()
    {
        IntBuffer id = Data.get().reserveDataInt(1);
        GL33.glGenSamplers(id);
        this.mID = id.get(0);
    }
    
    
    public final void param(
            final int aName, 
            final int aValue)
    {
        GL33.glSamplerParameteri(
            this.mID, 
            aName, 
            aValue
        );
    }
    
    public void onEnd()
    {
        GL33.glDeleteSamplers(this.mID);
    }
    
}
